package com.tianzeculture.stagebao.activity.login;

import android.content.Intent;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.application.StageBaoApplication;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.dialog.ChoiceJobDialog;
import com.tianzeculture.stagebao.net.UserRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.Util;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/23 0023.
 * 注册
 */

public class RegisterActivity extends BaseActivity {

    ChoiceJobDialog choiceJobDialog = null;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvPact)
    TextView tvPact;
    @BindView(R.id.btnRegister)
    TextView btnRegister;
    @BindView(R.id.tvGetCode)
    TextView tvGetCode;

    @BindView(R.id.edtPhone)
    EditText edtPhone;
    @BindView(R.id.edtCode)
    EditText edtCode;
    @BindView(R.id.edtPwd)
    EditText edtPwd;
    @BindView(R.id.edtPwdAgain)
    EditText edtPwdAgain;
    @BindView(R.id.cboxPact)
    CheckBox cboxPact;

    private int GET_CODE = 101;//获取验证码
    private int REGISTER = 102;//注册
    private String phone = "";//获取验证码的手机号
    private String code = "";//验证码

    @Request
    UserRestUsage userRestUsage = null;


    TimeCount time;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_register;
    }

    @Override
    public void initUI() {
        super.initUI();
        StageBaoApplication.addActivity(this);

        tvTitle.setText(getString(R.string.registered));
        tvPact.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tvPact.getPaint().setAntiAlias(true);//抗锯齿
        tvPact.setText(getString(R.string.pact));


        time = new TimeCount(10000, 1000);
    }

    @OnClick(R.id.tvGetCode)
    public void getCode(View view) {
        if (Util.isPhone(edtPhone.getText().toString().trim())) {//1注册 2重置密码
            phone = edtPhone.getText().toString().trim();//保存获取验证码的手机号
            userRestUsage.getCode(GET_CODE, edtPhone.getText().toString().trim(), 1);
            time.start();
        }
    }

    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
//            tvGetCode.setBackgroundColor(getColor(R.color.colorGray_D7));
            tvGetCode.setClickable(false);
//            tvGetCode.setText("(" + millisUntilFinished / 1000 + ") 秒后可重新发送");
            tvGetCode.setText(millisUntilFinished / 1000 + "s");
        }

        @Override
        public void onFinish() {
            tvGetCode.setText("重新获取验证码");
            tvGetCode.setClickable(true);
//            tvGetCode.setBackgroundColor(getColor(R.color.colorGray_777));
        }
    }


    /**
     * 非空判断
     *
     * @return
     */
    private boolean checkEntity() {
        if ("".equals(edtPhone.getText().toString().trim())) {
            ToastUtil.showShort(RegisterActivity.this, "手机号码不能为空!");
            return false;
        } else if ("".equals(edtCode.getText().toString().trim())) {
            ToastUtil.showShort(RegisterActivity.this, "验证码不能为空!");
            return false;
        } else if ("".equals(edtPwd.getText().toString().trim())) {
            ToastUtil.showShort(RegisterActivity.this, "密码不能为空!");
            return false;
        } else if ("".equals(edtPwdAgain.getText().toString().trim())) {
            ToastUtil.showShort(RegisterActivity.this, "再次密码不能为空!");
            return false;
        } else if (!edtPwd.getText().toString().trim().equals(edtPwdAgain.getText().toString().trim())) {
            ToastUtil.showShort(RegisterActivity.this, "两次密码不一致!");
            return false;
        } else if (!cboxPact.isChecked()) {
            ToastUtil.showShort(this, "请阅读并同意舞台宝协议！");
        }
        return true;
    }

    @OnClick(R.id.btnRegister)
    public void register() {
        if (checkEntity()) {
            if (!edtCode.getText().toString().trim().equals(code)) {
                ToastUtil.showShort(this, "验证码错误!");
                return;
            } else if (!phone.equals(edtPhone.getText().toString().trim())) {
                ToastUtil.showShort(this, "该手机号码与获取验证码手机不匹配!");
                return;
            }
            choiceJobDialog = new ChoiceJobDialog(this);
            choiceJobDialog.setChoiceState(0);//未选择
            choiceJobDialog.setOnSelectClickListener(new ChoiceJobDialog.OnSelectClickListener() {
                @Override
                public void onOk(int state) {
                    if (state == 1) {
                        ToastUtil.showShort(RegisterActivity.this, "艺人");
                        showProgressDialog("正在注册...");
                        userRestUsage.Register(REGISTER, phone,
                                edtCode.getText().toString().trim(), edtPwd.getText().toString().trim(), 0);
                    } else {
                        ToastUtil.showShort(RegisterActivity.this, "商家");
                        showProgressDialog("正在注册...");
                        userRestUsage.Register(REGISTER, phone,
                                edtCode.getText().toString().trim(), edtPwd.getText().toString().trim(), 1);
                    }
//                    startActivity(new Intent(RegisterActivity.this, PerfectPersonInfoActivity.class));
                }

                @Override
                public void onCancel() {

                }
            });
            choiceJobDialog.show();
        }

    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        if (taskId == GET_CODE) {
            if (msg.getObj() != null) {
                ResultBean resultBean = (ResultBean) msg.getObj();
                code = resultBean.getCode();
//                ToastUtil.showShort(this, code);
            }
        } else if (taskId == REGISTER) {
            if (msg.getData()) {
                if (msg.getObj() != null) {
                    ResultBean resultBean = (ResultBean) msg.getObj();
                    Util.Token = resultBean.getToken();
                    Util.Uid = resultBean.getUserId();
                    startActivity(new Intent(RegisterActivity.this, PerfectPersonInfoActivity.class));
                }
            }
            dismissProgressDialog();
        }
    }
}
